//
//  LayoutCellTable.m
//  ebook
//
//  Created by 吴超飞 on 2017/3/14.
//  Copyright © 2017年 Moton. All rights reserved.
//

#import "LayoutCellTable.h"

#define LayoutSpaceIn  (MTScreenW * 0.05) //内部边距
#define ColumnCount (isIpad?6:4)  //列数
#define ItemWidth ((MTScreenW-2*LayoutSpaceOut-(ColumnCount-1)*LayoutSpaceIn)/ColumnCount)  //每本书的宽度
#define CoverImageHeight 1.3*ItemWidth //封面图片的高度
#define BookNameHeight 40 //书名高度
#define ItemHeight (CoverImageHeight+BookNameHeight)

@interface LayoutCellTable()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation LayoutCellTable

- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    [self addSubview:self.collectionView];
}

- (UICollectionView *)collectionView {
   
    if (!_collectionView) {
       
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = LayoutSpaceIn;
        layout.itemSize = CGSizeMake(ItemWidth, ItemHeight);
        layout.sectionInset = UIEdgeInsetsMake(0, LayoutSpaceOut, 0, LayoutSpaceOut);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, MTScreenW, ItemHeight) collectionViewLayout:layout];
        _collectionView.backgroundColor = self.backgroundColor;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[UICollectionViewCell class]  forCellWithReuseIdentifier:kCellId];
    }
    
    return _collectionView;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
   
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.bookCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellId forIndexPath:indexPath];
    
    for (UIView *view in cell.subviews) {
        [view removeFromSuperview];
    }
    StoreBookModel *model = self.dataArray.count>indexPath.row?self.dataArray[indexPath.row]:nil;
    
    UIImageView *backImv = [[UIImageView alloc] init];
    backImv.frame = CGRectMake(0, 0, cell.width, cell.height-40);
    backImv.image = [UIImage imageNamed:@"bookcellBackground"];
    [cell addSubview:backImv];
    
    UIImageView *imv = [[UIImageView alloc] init];
    imv.frame = CGRectMake(2, 2, CGRectGetWidth(backImv.frame)-4, CGRectGetHeight(backImv.frame)-4);
    [backImv addSubview:imv];
    [imv sd_setImageWithURL:[NSURL URLWithString:model.Coverimg]];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, cell.height-40, cell.width, 40)];
    title.font = [UIFont systemFontOfSize:14.0f];
    title.textAlignment = NSTextAlignmentCenter;
    title.numberOfLines = 2;
    [cell addSubview:title];
    title.text = model.Name;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(layoutCellBase:didSelectModel:)]) {
        StoreBookModel *model = self.dataArray[indexPath.row];
        [self.delegate layoutCellBase:self didSelectModel:model];
    }
}

-(void)reloadData{
    
    CGRect frame = CGRectMake(0, _isNoHead?0:TitleHeight, MTScreenW, [LayoutCellTable viewHeight:self.layOutType isNoHead:YES]);//高度只取底部数据的部分，所以取值为YES
    self.collectionView.frame = frame;
    
    [self.collectionView reloadData];
}

+(float)viewHeight:(LayoutType *)layoutType isNoHead:(BOOL)isNohead{
    
    return (isNohead?0:TitleHeight) + ItemHeight*layoutType.rows + LayoutSpaceOut*(layoutType.rows-1);
}

+(float)viewHeight:(LayoutType *)layoutType{

    return [LayoutCellTable viewHeight:layoutType isNoHead:NO];
}

//一行的高度
+(float)oneRowHeight{

    return ItemHeight;
}


@end
